package cc.z8g.browser.database.bookmark;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cc.z8g.browser.core.BrowserConst;
import cc.z8g.browser.database.Bookmark;
import cc.z8g.browser.log.Logger;

/**
 * NetscapeBookmarkImporter
 *
 * @author zhaoxuyang.net
 * @since 2024-08-06
 */
class NetscapeBookmarkImporter {

    public static final String ITEM_TAG = "DT";
    public static final String LIST_TAG = "DL";
    public static final String BOOKMARK_TAG = "A";
    public static final String FOLDER_TAG = "H3";
    public static final String HREF = "HREF";
    public static final String ICON = "ICON";
    public static final String ADD_DATE = "ADD_DATE";
    public static final String ROOT_FOLDER_NAME = "";

    @NonNull
    public static List<Bookmark.Entry> importBookmarks(@NonNull final InputStream inputStream) {
        List<Bookmark.Entry> result = new ArrayList<>();
        try {
            XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
            parser.setInput(inputStream, null);

            int type;
            while ((type = parser.getEventType()) != XmlPullParser.END_DOCUMENT) {
                if (type == XmlPullParser.START_TAG) {
                    String tagName = parser.getName();
                    if ("DL".equals(tagName)) { // rootList
                        processFolder(parser, result, ROOT_FOLDER_NAME);
                        break; // 只有一个 rootList
                    }
                }
            }
            inputStream.close();
        } catch (Exception ex) {
            Logger.e("NetscapeBookmarkImporter", "#importBookmarks catch error", ex);
        }
        return result; // 能解析出多少算多少
    }

    private static void processFolder(XmlPullParser parser, List<Bookmark.Entry> result, String folderName) {
        String url = getAttr(parser, HREF);
        String title = getAttr(parser, ADD_DATE); // text
        int position = 0;
        String h3 = getAttr(parser, FOLDER_TAG);
        String icon = getAttr(parser, ICON);
        String addDate = getAttr(parser, ADD_DATE);
        Bookmark.Entry entry = new Bookmark.Entry(url, title, position, asFolder(h3), icon, addDate);
        result.add(entry);

        String nextFolderName = buildFolderName(folderName, h3);
        processFolder(parser, result, nextFolderName);
    }

    private static String getAttr(XmlPullParser parser, final String attrName) {
        return null;
    }

    private static String buildFolderName(String parentFolder, String currFolder) {
        if (TextUtils.isEmpty(parentFolder)) {
            return currFolder;
        } else {
            return parentFolder + "/" + currFolder;
        }
    }

    private static Bookmark.Folder asFolder(String folderName) {
        if (!TextUtils.isEmpty(folderName)) {
            return new Bookmark.Folder.Entry(BrowserConst.FOLDER + folderName, folderName);
        }
        return Bookmark.Folder.Root.INSTANCE;
    }
}
